package cn.bdqn.service.questionBank;

import cn.bdqn.dao.questionBank.TestPaperDao;
import cn.bdqn.dto.questionBank.subject.SubjectAccountTypeDTO;
import cn.bdqn.dto.questionBank.test_paper.TestPaperInsertDTO;
import cn.bdqn.dto.questionBank.test_paper.TestPaperInsertSubjectIdDTO;
import cn.bdqn.dto.questionBank.test_paper.TestPaperPageDTO;
import cn.bdqn.dto.questionBank.test_paper.TestPaperSubjectIsStatusCountDTO;
import cn.bdqn.pojo.questionBank.OperationRecord;
import cn.bdqn.pojo.questionBank.Subject;
import cn.bdqn.pojo.questionBank.TestPaper;
import cn.bdqn.utils.Json;
import cn.bdqn.vo.questionBank.subject.SubjectVo;
import cn.bdqn.vo.questionBank.test_paper.TestPaperVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("testPaperService")
public class TestPaperService {
    @Autowired
    private TestPaperDao testPaperDao;
    @Autowired
    private SubjectService subjectService;

    @Autowired
    private OperationRecordService operationRecordService;
    /**
     * 分页查询
     * @param testPaperPageDTO
     * @return
     */
    public Json selectPageTestPaper(TestPaperPageDTO testPaperPageDTO){
        PageHelper.startPage(testPaperPageDTO.getPageNumber(),testPaperPageDTO.getPageSize());
        List<TestPaperVo> testPaperList = testPaperDao.selectPageTestPaper(testPaperPageDTO.getStatus(),testPaperPageDTO.getTestPaperTypeId(),testPaperPageDTO.getAccountTypeId());
        PageInfo<TestPaperVo> page = new PageInfo<>(testPaperList);
        return new Json(200,"查询成功",page);
    }

    /**
     * 查询该试卷的所有题目
     * @param testPaperId
     * @return
     */
    public Json selectSubjectByTestPaperId(Integer testPaperId){
        List<Subject> subjectList = testPaperDao.selectSubjectByTestPaperId(testPaperId);
        if(subjectList == null && subjectList.size() <=0){
            return Json.fall("题目查找失败");
        }
        return Json.ok(subjectList);
    }

    /**
     * 添加试卷功能
     * @param userId
     * @param testPaperInsertDTO
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public Json insertTestPaper(Integer userId, TestPaperInsertDTO testPaperInsertDTO) throws Exception {
        OperationRecord operationRecord = operationRecordService.operationRecordPublicMethod(userId,"添加一张试卷",new SubjectAccountTypeDTO());
        if(operationRecord == null){
            throw new Exception("操作记录失败");
        }
        //单选的题目
        List<Integer> dxList = subjectService.getSubjectIdList(testPaperInsertDTO.getTestPaper().getAccountTypeId(),1,testPaperInsertDTO.getOneSelectSubjectNumber());
        //判断
        List<Integer> pdList = subjectService.getSubjectIdList(testPaperInsertDTO.getTestPaper().getAccountTypeId(),2,testPaperInsertDTO.getIsSelectSubjectNumber());
        //多选
        List<Integer> dxLists = subjectService.getSubjectIdList(testPaperInsertDTO.getTestPaper().getAccountTypeId(),3,testPaperInsertDTO.getMachSelectSubjectNumber());
        dxList.addAll(pdList);
        dxList.addAll(dxLists);
        testPaperInsertDTO.getTestPaper().setIsDel(1);
        testPaperInsertDTO.getTestPaper().setStatus(2);
        testPaperInsertDTO.getTestPaper().setCreateOperationRecordId(operationRecord.getOperationRecordId());
        testPaperInsertDTO.getTestPaper().setUpdateOperationRecordId(0);
        int a = testPaperDao.insert(testPaperInsertDTO.getTestPaper());
        if(a != 1){
            throw new Exception("试卷添加失败");
        }
        int b = testPaperDao.insertTestPaperSubject(testPaperInsertDTO.getTestPaper().getTestPaperId(),dxList);
        if(b != dxList.size()){
            throw new Exception("试卷题目添加失败");
        }
        //System.err.println(b);
        return selectPageTestPaper(testPaperInsertDTO.getTestPaperPageDTO());
    }


    /**
     * 查询题目和试卷发布和未发布统计
     * @return
     */
    public Json selectCountNumber(){
        List<Integer> list = testPaperDao.selectCountNumber();
        if(list.size() != 4){
            return Json.fall("为获取到");
        }
        TestPaperSubjectIsStatusCountDTO testPaperSubjectIsStatusCountDTO = new TestPaperSubjectIsStatusCountDTO();
        testPaperSubjectIsStatusCountDTO.setSubjectIsStatusCount(list.get(0));
        testPaperSubjectIsStatusCountDTO.setSubjectNotStatusCount(list.get(1));
        testPaperSubjectIsStatusCountDTO.setTestPaperIsStatusCount(list.get(2));
        testPaperSubjectIsStatusCountDTO.setTestPaperNotStatusCount(list.get(3));
        return Json.ok(testPaperSubjectIsStatusCountDTO);
    }

    /**
     * 添加试卷功能，用户自己选题
     * @param testPaperInsertSubjectIdDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Json insertTestPaperBySubjectListId(Integer userId,TestPaperInsertSubjectIdDTO testPaperInsertSubjectIdDTO) throws Exception {
        OperationRecord operationRecord = operationRecordService.operationRecordPublicMethod(userId,"添加试卷",new SubjectAccountTypeDTO());
        if(operationRecord == null){
            throw new Exception("操作记录失败");
        }
        testPaperInsertSubjectIdDTO.getTestPaper().setIsDel(1);
        testPaperInsertSubjectIdDTO.getTestPaper().setStatus(2);
        testPaperInsertSubjectIdDTO.getTestPaper().setCreateOperationRecordId(operationRecord.getOperationRecordId());
        testPaperInsertSubjectIdDTO.getTestPaper().setUpdateOperationRecordId(0);
        int a = testPaperDao.insert(testPaperInsertSubjectIdDTO.getTestPaper());
        if(a != 1){
            throw new Exception("试卷添加失败");
        }
        int b = testPaperDao.insertTestPaperSubject(testPaperInsertSubjectIdDTO.getTestPaper().getTestPaperId(),testPaperInsertSubjectIdDTO.getUserSelectSubjectId());
        if(b != testPaperInsertSubjectIdDTO.getUserSelectSubjectId().size()){
            throw new Exception("试卷题目添加失败");
        }
        return selectPageTestPaper(testPaperInsertSubjectIdDTO.getTestPaperPageDTO());
    }

    /**
     * 批量删除试卷
     * @param userId
     * @param testPaperPageDTO
     * @param list
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public Json deleteTestPaperList(Integer userId,TestPaperPageDTO testPaperPageDTO,List<Integer> list) throws Exception {
        if(list.size() == 0){
            throw new Exception("无数据删除");
        }
        OperationRecord operationRecord = operationRecordService.operationRecordPublicMethod(userId,"批量删除试卷",new SubjectAccountTypeDTO());
        if(operationRecord == null){
            throw  new Exception("操作记录失败");
        }

        int a = testPaperDao.deleteTestPaperList(list);
        if(a != list.size()){
            throw new Exception("试卷批量删除失败");
        }
        return selectPageTestPaper(testPaperPageDTO);
    }
}
